package com.cjj.navtong;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.cjj.navtong.adapter.TaskListAdapter;
import com.cjj.navtong.db.Child;
import com.cjj.navtong.db.DBAdapter;
public class TaskListActivity extends Activity {
	
	private ListView mListView;
	private TaskListAdapter mAdapter;
	
	public static final int TYPE_INTERNET = 0X0001;
	public static final int TYPE_LOCAL = 0X0002;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_list);
		mListView = (ListView) findViewById(R.id.listView1);
		initData(getIntent().getIntExtra("type", TYPE_INTERNET));
	}
	/**
	 * 根据输入类型加载任务列表
	 * @param type 
	 */
	public void initData(int type){
		if(type == TYPE_INTERNET){
			//从网络加载任务列表
			new UdpSocketRequest().start();
		}else if(type == TYPE_LOCAL){
			//从本地加载任务列表
			getDataFromLocal();
		}
	}
	
	private void getDataFromLocal(){
		reflashList(DBAdapter.getInstance(this).queryAllData());
	}
	
	private void reflashList(Child[] children){
		if(null == children || children.length == 0){
			return;
		}
		mAdapter = new TaskListAdapter(TaskListActivity.this);
		for (Child child : children) {
			mAdapter.add(child);
		}
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent i = new Intent(TaskListActivity.this, TaskInfoActivity.class);
				i.putExtra("child", mAdapter.getItem(arg2));
				startActivity(i);
			}
		});
		
	}
	
	private void addToList(Child[] children){
		if(null  == mAdapter){
			reflashList(children);
		}else{
			for (Child child : children) {
				mAdapter.add(child);
			}
			mAdapter.notifyDataSetChanged();
		}
	}
	
	
	
	public class UdpSocketRequest extends Thread {
		@Override
		public void run() {
			//String serverHost = "192.168.1.104";
			int serverPort = 3344;
			UDPReceiveStr udpReceiveStr;
			//Toast.makeText(Taskmanage.this, "chujingjinghahahh",Toast.LENGTH_LONG).show();
			//System.out.println(Thread.currentThread().getName()+"nothing");
			try {
				
				udpReceiveStr = new UDPReceiveStr(serverPort,mHandler);
				
				while (true) {
					
					udpReceiveStr.receive();
					udpReceiveStr.response("你好,已收到!");
					
				}
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
	}
	private Handler mHandler = new Handler(Looper.getMainLooper()){
		public void handleMessage(android.os.Message msg) {
//			showTask.append("\r\n"+msg.obj.toString());
			ArrayList<Child> list = (ArrayList<Child>) msg.obj;
			Child[] children = list.toArray(new Child[0]); 
			//更新任务列表
			addToList(children);
			//存储在本地数据库中
			DBAdapter.getInstance(TaskListActivity.this).insert(children);
		};
	};
}
